package test.simple;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

//http://camel.apache.org/log.html
//http://camel.apache.org/jetty.html
//http://exampledepot.com/egs/java.net/GetHeaders.html
public class LaunchJettyPayload {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		CamelContext context = new DefaultCamelContext();
		context.addRoutes(new RouteBuilder() {
			public void configure() {
				//http://camel.apache.org/log.html
				//http://camel.apache.org/jetty.html
				from("jetty:http://localhost:8976/test").to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true").
				setBody(constant("Response 1\nResponse 2")).setHeader("testHeader", constant("testHeaderValue"));
			}
		});
		context.start();
	      URL u;
	      InputStream is = null;
	      DataInputStream dis;
	      String s;

	      try {

	         //------------------------------------------------------------//
	         // Step 2:  Create the URL.                                   //
	         //------------------------------------------------------------//
	         // Note: Put your real URL here, or better yet, read it as a  //
	         // command-line arg, or read it from a file.                  //
	         //------------------------------------------------------------//

	         u = new URL("http://localhost:8976/test?xml=3241526&type=tst");
	        // u = new URL("http://intra.tander");
	         //----------------------------------------------//
	         // Step 3:  Open an input stream from the url.  //
	         //----------------------------------------------//
	         URLConnection conn = u.openConnection();
	         
	         // List all the response headers from the server.
	         // Note: The first call to getHeaderFieldKey() will implicit send
	         // the HTTP request to the server.
	         for(String ss:conn.getHeaderFields().keySet()){
	        	 System.out.println(ss+" "+conn.getHeaderField(ss));
	     
	         }
	         is = conn.getInputStream();

	         //-------------------------------------------------------------//
	         // Step 4:                                                     //
	         //-------------------------------------------------------------//
	         // Convert the InputStream to a buffered DataInputStream.      //
	         // Buffering the stream makes the reading faster; the          //
	         // readLine() method of the DataInputStream makes the reading  //
	         // easier.                                                     //
	         //-------------------------------------------------------------//

	         dis = new DataInputStream(new BufferedInputStream(is));

	         //------------------------------------------------------------//
	         // Step 5:                                                    //
	         //------------------------------------------------------------//
	         // Now just read each record of the input stream, and print   //
	         // it out.  Note that it's assumed that this problem is run   //
	         // from a command-line, not from an application or applet.    //
	         //------------------------------------------------------------//
	         System.out.println("=================");
	         System.out.println("==== Jetty Response ===");
	         while ((s = dis.readLine()) != null) {
	            System.out.println(s);
	         }

	      } catch (MalformedURLException mue) {

	         System.out.println("Ouch - a MalformedURLException happened.");
	         mue.printStackTrace();
	         System.exit(1);

	      } catch (IOException ioe) {

	         System.out.println("Oops- an IOException happened.");
	         ioe.printStackTrace();
	         System.exit(1);

	      } finally {

	         //---------------------------------//
	         // Step 6:  Close the InputStream  //
	         //---------------------------------//

	         try {
	            is.close();
	         } catch (IOException ioe) {
	            // just going to ignore this one
	         }

	      } // end of 'finally' clause		
		Thread.sleep(5000);
		context.stop();
	}

}
